using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Locations;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.Util;
using Tracker.Mobile.Droid.Tasks;

namespace Tracker.Mobile.Droid.Services
{
    [Service]
    public class LocationService : Service
    {
        private ISharedPreferences _preferences;
        private LocationTask _locationTask;

        private int _minutes;

        private const long Second = 1000;
        private const long MinuteSeconds = 60 * Second;

        public override IBinder OnBind(Intent intent)
        {
            throw new NotImplementedException();
        }

        public override void OnCreate()
        {
            _preferences = GetSharedPreferences(Global.Preferences, FileCreationMode.Private);
            _minutes = _preferences.GetInt(PreferenceKeys.Minutes, 1);

            Timer timer = new Timer();
            long period = _minutes * MinuteSeconds;

            _locationTask = new LocationTask(this);
            timer.ScheduleAtFixedRate(_locationTask, 5000, period);

            base.OnCreate();
        }

        public override void OnStart(Intent intent, int startId)
        {
            base.OnStart(intent, startId);
        }

        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            return StartCommandResult.Sticky;
        }

        public override void OnDestroy()
        {
            _preferences.Edit().PutBoolean(PreferenceKeys.ServiceRunning, false).Commit();
            base.OnDestroy();
        }
    }
}